//
//  BGJieTiJiaGeController.m
//  bigMall
//
//  Created by ios on 16/8/4.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGJieTiJiaGeController.h"
#import "BGJieTiJiaGeCell.h"
#import "BGDateResult.h"
#import "BGAddStepPricesModel.h"
#import "BGFaBuViewController.h"
#define fujiaxinxi @"f"

@interface BGJieTiJiaGeController () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    
    UITableView *_tableView;
    int index;//表格增加索引值
    NSMutableArray *arr;
    NSMutableArray *arr3;
    UIButton *_btn;
    
}

@end

@implementation BGJieTiJiaGeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"阶梯价格";
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];
    self.navigationItem.rightBarButtonItem = right;
    
    [self initDat];
    [self createUi];
    
}

- (void)initDat {
    
    index = 1;
    
}

- (void)createUi {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight - 71) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到view上
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BGWidth - 42, 14)];
    
    lab.textColor = [UIColor lightGrayColor];
    
    lab.text = @"填写吨数和价格，如300吨以上（含），6000元/吨";
    
    lab.font = [UIFont systemFontOfSize:12];
    
    lab.textAlignment = NSTextAlignmentCenter;
    
    _tableView.tableFooterView = lab;
    
    //    _tableView.allowsSelectionDuringEditing = YES;
    [self.view addSubview:_tableView];
    
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height - 60, self.view.bounds.size.width - 40, 50)];
    
    _btn.clipsToBounds = YES;
    
    [_btn setTitle:@"确定" forState:UIControlStateNormal];
    
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _btn.layer.cornerRadius = 10;
    
    [_btn setBackgroundColor:[UIColor redColor]];
    
    [_btn addTarget:self action:@selector(queding) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_btn];
    
}
- (void)queding {
    
    arr = [NSMutableArray array];
    arr3 = [NSMutableArray array];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    
    for (BGJieTiJiaGeCell *cel in _tableView.visibleCells) {
        
        [dic setValue:cel.shuliang.text forKey:@"number"];
        [dic setValue:cel.jiage.text forKey:@"price"];
        
        [arr addObject:dic];
        
    }
    
    
    [BGDateResult addStepPricesWithStepPrices:arr success:^(NSObject *result) {
        
        BGAddStepPricesModel *model = [[BGAddStepPricesModel alloc] init];
        
        model = (BGAddStepPricesModel *)result;
        
        if ([model.status isEqualToString:@"1"]) {
            
            BGFaBuViewController *a = [BGFaBuViewController share];
            a.stepStr = model.ids;
            
            [self.navigationController popViewControllerAnimated:YES];
            NSLog(@"%@",model.ids);
        }else {
            [self showMessage:model.msg];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
//    
//    [[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"fujiaxinxi"];
//    
//    [[NSUserDefaults standardUserDefaults] setObject:arr3 forKey:@"fujiaxinxi3"];
//    
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:fujiaxinxi];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
//    NSLog(@"%@",arr);
//    NSLog(@"%@",arr3);
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)add {
    
    index++;
    [_tableView reloadData];
    
}

#pragma mark - dataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:fujiaxinxi] isEqualToString:@"1"]) {
//        return arr.count;
//    }else {
    
    return index;
    
//    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 127;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 12;
    
}

#pragma mark - delegate -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView registerNib:[UINib nibWithNibName:@"BGJieTiJiaGeCell" bundle:nil] forCellReuseIdentifier:@"celId"];
    BGJieTiJiaGeCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    cel.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cel.shuliang.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    cel.jiage.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    if ([fujiaxinxi isEqualToString:@"1"]) {
        
        
        
//        cel.shuliang.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"fujiaxinxi"][indexPath.row];
//        cel.jiage.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"fujiaxinxi3"][indexPath.row];
        
    }
    
    cel.shuliang.delegate = self;
    
    return cel;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        index--;
        NSArray *indexPaths = @[indexPath]; // 构建 索引处的行数 的数组
        //        // 删除 索引的方法 后面是动画样式
        [_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationLeft)];
        //真正项目中做删除
        
        //1.将表中的cell删除
        
        //2.将本地的数组中数据删除
        
        //3.最后将服务器端的数据删除
        
    }
    
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
    
}
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    
    for (BGJieTiJiaGeCell *cell in _tableView.visibleCells) {
        // cell的数据
        [cell.shuliang resignFirstResponder];
        [cell.jiage resignFirstResponder];
        
    }
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [BGFaBuViewController share].type = @"jieti";
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
